package com.w.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.java.emoji.EmojiConverter;
import com.w.wx.domain.*;
import com.w.wx.mapper.NoticeMapper;
import com.w.wx.service.CommeService;
import com.w.wx.service.ImagesService;
import com.w.wx.service.UserService;
import com.w.wx.service.impls.CommeServiceImpl;
import com.w.wx.utils.ALToHMUtil;
import com.w.wx.utils.SensitivewordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/wx/pl/")
public class CommentsContro {

    @Autowired
    private CommeServiceImpl commeService;//我怎么知道调用的是哪个实现类的方法
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private UserService userService;
    @Autowired
    private NoticeMapper noticeMapper;
    /**
     * 发布主评论,并返回发布的信息

     * @return
     */
    @PostMapping("addMainComments")
    public Msg addMainComments(@RequestBody JSONObject info){
        JSONObject info1 = info.getJSONObject("info");
        CommentsInfo Maincomment_info = JSONObject.toJavaObject(info1, CommentsInfo.class);
        //判断用户是否被拉黑
        UserEr userInfo = userService.getUserInfo(Maincomment_info.getFromOpenid());
        if (userInfo.getState() == 10){
            Notice notice =noticeMapper.selectByTwoOpenid(userInfo.getOpenid());
            return Msg.fail().add("notice",notice);
        }



        //敏感词判断
        SensitivewordFilter sensitivewordFilter = new SensitivewordFilter();
        String content = Maincomment_info.getContent();
        if(sensitivewordFilter.isContaintSensitiveWord(content,1)){
            Set<String> sensitiveWord = sensitivewordFilter.getSensitiveWord(content, 1);
            return Msg.fail().add("error","包含敏感词"+sensitiveWord.toString());
        }

        Maincomment_info.setContent(ALToHMUtil.toHtml(content));
        CommentsInfo commentsInfo = commeService.addMainComments(Maincomment_info);
        if(commentsInfo==null){
            return Msg.fail();
        }else {
            //评论通知
            imagesService.addGooddNotice(Maincomment_info);
            //刚发布当前用户点赞状态为0
            commentsInfo.setLikeState(0);
            ArrayList<CommentsReply> commentsReplies = new ArrayList<>();
            commentsReplies.add(new CommentsReply("without"));
            commentsInfo.setCommentsReplies(commentsReplies);

           commentsInfo.setContent(ALToHMUtil.toUnicode(content));
            return Msg.success().add("commentsInfo",commentsInfo);
        }

    }

    /**
     * 发布子评论，,并返回发布的信息

     * @return
     */
    @PostMapping("addSonCommments")
    public Msg addSonCommments(@RequestBody CommentsReply Vicecomment_info){
        //判断用户是否被拉黑
        UserEr userInfo = userService.getUserInfo(Vicecomment_info.getFromOpenid());
        if (userInfo.getState() == 10){
            Notice notice =noticeMapper.selectByTwoOpenid(userInfo.getOpenid());
            return Msg.fail().add("notice",notice);
        }

        //敏感词判断
        SensitivewordFilter sensitivewordFilter = new SensitivewordFilter();
        String content = Vicecomment_info.getContent();
        if(sensitivewordFilter.isContaintSensitiveWord(content,1)){
            Set<String> sensitiveWord = sensitivewordFilter.getSensitiveWord(content, 1);
            return Msg.fail().add("error","包含敏感词"+sensitiveWord.toString());
        }

        Vicecomment_info.setCreateTime(new Date());
        Vicecomment_info.setLikeNum(0);

        Vicecomment_info.setContent(ALToHMUtil.toHtml(content));
        CommentsReply commentsReply = commeService.addSonComments(Vicecomment_info);
        if(commentsReply==null){
            return Msg.fail();
        }else {
            //刚发布当前用户点赞状态为0
            commentsReply.setLikeState(0);
            commentsReply.setContent(ALToHMUtil.toUnicode(content));
            return Msg.success().add("commentsReply",commentsReply);
        }
    }

    /**
     * 获取当前gid的全部的评论回复
     * @param gid
     * @return
     */
    @RequestMapping("getAllComments")
    public ArrayList<CommentsInfo> getAllComments(int gid,String openid){

        ArrayList<CommentsInfo> arrayList = commeService.getAllComments(gid,openid);
        Iterator<CommentsInfo> it = arrayList.iterator();
        while (it.hasNext()){
            //转化表情
            CommentsInfo next = it.next();
            next.setContent(ALToHMUtil.toUnicode(next.getContent()));

        }
       return arrayList;
    }

    /**
     * 获取子评论的数量
     * @param gid
     * @return
     */
    @RequestMapping("getSonCommentsSums")
    public Msg getSonCommentsSums(int gid){
        int sum = commeService.getSonCommentsSums(gid);
        return Msg.success().add("aaa",sum);
    }

    /**
     * 获取一条主评论和它的所有子评论
     * @param inId
     * @return
     */
    @RequestMapping("getMainAllSonCom")
    public Msg getMainAllSonCom(int inId,String openid){

        CommentsInfo commentsInfo = commeService.getMainAllSonCom(inId,openid);
        return Msg.success().add("commentsInfo",commentsInfo);
    }

}
